package com.yonyou.sto.modules.ims.controller;


import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.yonyou.sto.common.api.CommonPage;
import com.yonyou.sto.common.api.CommonResult;
import com.yonyou.sto.common.domain.DownloadFile;
import com.yonyou.sto.modules.ims.dto.TaxItemDto;
import com.yonyou.sto.modules.ims.model.ImsBuyer;
import com.yonyou.sto.modules.ims.model.ImsParty;
import com.yonyou.sto.modules.ims.service.ImsBuyerService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.InputStream;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author aegon
 * @since 2020-09-28
 */
@RestController
@RequestMapping("/server/ims/imsBuyer")
@Api(tags = "ImsBuyerController", description = "付款单位管理")
public class ImsBuyerController {

    @Resource
    private ImsBuyerService buyerService;

    @ApiOperation("新增")
    @PostMapping(value = "/create")
    public CommonResult create(@RequestBody ImsBuyer buyer) {
        return buyerService.create(buyer);
    }

    @ApiOperation(value = "详情")
    @GetMapping(value = "/info/{buyerTaxSn}")
    public CommonResult info(@PathVariable String buyerTaxSn) {
        return CommonResult.success(buyerService.getBuyerByBuyerTaxSn(buyerTaxSn));
    }

    @ApiOperation("修改")
    @PostMapping(value = "/update/{id}")
    public CommonResult update(@PathVariable Long id, @RequestBody ImsBuyer buyer) {
        buyer.setId(id);
        return CommonResult.success(buyerService.updateById(buyer));
    }

    @ApiOperation("删除")
    @PostMapping(value = "/delete")
    public CommonResult delete(@RequestParam Long id) {
        return buyerService.delete(id);
    }

    @ApiOperation(value = "根据组织编码名称分页查询组织")
    @GetMapping(value = "/list")
    public CommonResult<CommonPage<ImsBuyer>> list(@RequestParam(value = "keyword", required = false) String keyword,
                                                   @RequestParam(value = "pageSize", defaultValue = "5") Integer pageSize,
                                                   @RequestParam(value = "pageNum", defaultValue = "1") Integer pageNum) {
        Page<ImsBuyer> buyerPage = buyerService.buyerList(keyword, pageSize, pageNum);
        return CommonResult.success(CommonPage.restPage(buyerPage));
    }

    @ApiOperation(value = "下载导入模板")
    @GetMapping(value = "/downloadFile")
    public void downloadFile(HttpServletRequest request, HttpServletResponse response){
        try {
            InputStream is = this.getClass().getClassLoader().getResourceAsStream("buyerTemplate.xlsx");
            byte[] fileData = DownloadFile.input2byte(is);
            DownloadFile.downloadFile(response, request, "buyerTemplate.xlsx", fileData);
        } catch (IOException ioException) {
            ioException.printStackTrace();
        }
    }

    @ApiOperation(value = "买方导入excel")
    @PostMapping(value = "/importBuyer")
    public CommonResult importBuyer(MultipartFile file) {
        return buyerService.importBuyer(file);
    }

}

